package cn.ad.server.web.spring.service.actions.context;

import cn.ad.server.web.spring.model.BidAdRequest;
import cn.ad.server.web.spring.model.BidAdResponse;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author dongbin.yu
 * @from 2016-07-18
 * @since V1.0
 */
public abstract class BidProcessor {

    protected ObjectMapper objectMapper;

    public abstract BidAdRequest parse(HttpServletRequest request);

    public abstract BidAdResponse doProcess(HttpServletRequest request, HttpServletResponse response, BidAdRequest bidAdRequest, BidContext bidContext);

    public abstract BidContext initBidData(BidAdRequest bidAdRequest);

    public BidAdResponse process(HttpServletRequest request, HttpServletResponse response){
        //解析参数，每家exchange都不太一样
        BidAdRequest bidAdRequest = parse(request);
        //TODO 生成BidContext
        BidContext bidContext = initBidData(bidAdRequest);

        return doProcess(request, response, bidAdRequest, bidContext);

    }

    @PostConstruct
    public void init() {

        objectMapper = new ObjectMapper();
        //配置null过滤掉
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.disable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    }

}
